package com.copa.l101;

/**
 * @author copa
 * @createDate 2022-07-13 17:52
 * @function 递归法
 */
public class Test2 {

    public boolean isSymmetric(TreeNode root) {
        return check(root, root);
    }

    private boolean check(TreeNode left, TreeNode right) {
        if (left == null && right == null) {
            // 如果左右子树都没有那就证明是一样的
            return true;
        }
        if (left == null || right == null) {
            // 如果左右子树有一边为空，那么必定会不相等
            return false;
        }
        // 比较两者的值，左右子树走的方向是相反的，左边走左则右边走右，左边走右则右边走左
        return left.val == right.val && check(left.left, right.right) && check(left.right, right.left);
    }

    public static void main(String[] args) {
        TreeNode root = new TreeNode(1);
        root.left = new TreeNode(2);
        root.left.left = new TreeNode(3);
        root.left.right = new TreeNode(4);
        root.right = new TreeNode(2);
        root.right.left = new TreeNode(4);
        root.right.right = new TreeNode(3);
        System.out.println(new Test2().isSymmetric(root));
    }
}
